<?php
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
error_reporting(E_ERROR);

// 获取ipad协议登录成功
function ipad_login_oper($_Data, $bot_data) {
	$result = ipad_login();
	$rt = array();
	if (empty($result) || $result['Code'] != 0) {
		$rt['state'] = -1;
		$rt['msg'] = '协议错误!';
	} else {
		foreach($result['Data'] as $key => $value) {
			$rt[$key] = $value;
		}

		$rt['state'] = 1;
		$rt['msg'] = '成功!';

		// 进行成功处理
		simQuery("UPDATE robot SET wechat_uuid_key = '".$rt["Uuid"]."' WHERE id = ".$_Data['id']);
	}
	return $rt;
}

// 获取ipad协议登录状态
function ipad_login_state($_Data, $bot_data) {
	$result = ipad_get_login_state($bot_data['wechat_uuid_key']);
	LOGINFO($result, '=== $result');
	if (empty($result) || $result['Code'] != 0) {
		return array (
			'state' => -4,
			'msg'	=> $result['Message']?$result['Message']:"没有登录状态信息!",
		);
	}

	if (empty($result['Data']) || empty($result['Data']['WxId'])) {
		return array (
			'state' => 0,
			'msg'	=> '正在登录过程中',
		);
	} else {
		$rt = array();
		foreach($result['Data'] as $key => $value) {
			$rt[$key] = $value;
		}
		$rt['state'] = 1;
		$rt['msg']	= '成功';
		simQuery("UPDATE robot SET wechat_nick_name = '".$rt['NickName']."', wechat_id = '".$rt['WxId']."', wechat_head_url = '".$rt['HeadUrl']."' WHERE id = ".$_Data['id']);
		return $rt;
	}
}

// 退出微信
function ipad_exit_oper($_Data, $bot_data) {
	$result = ipad_exit($bot_data['wechat_id']);
	if (empty($result) || empty($result['Code']) || $result['Code'] != 0) {
		simQuery("UPDATE robot SET wechat_uuid_key = '', wechat_nick_name = '', wechat_id = '', wechat_head_url = '' WHERE id = {$_Data['id']}");
		return array (
			'state' => 2,
			'msg'	=> $result['Message']?$result['Message']:"没有退出返回信息!",
		);
	}else {
		simQuery("UPDATE robot SET wechat_uuid_key = '', wechat_nick_name = '', wechat_id = '', wechat_head_url = '' WHERE id = {$_Data['id']}");
		return array (
			'state' => 1,
			'msg'	=>'成功!',
		);
	}
}

// 获取心跳
function ipad_heart_beat($_Data, $bot_data) {
	$result = ipad_get_heart_beat($bot_data['wechat_id']);
	if (empty($result) || $result['Code'] != 0) {
		simQuery("UPDATE robot SET wechat_uuid_key = '', wechat_nick_name = '', wechat_id = '', wechat_head_url = '' WHERE id = {$_Data['id']}");
		return array (
			'state' => -3,
			'msg'	=> '没有当前心跳!',
		);
	} else {
		return array (
			'state' => 1,
			'msg'	=> '成功!',
		);
	}
}

// 同步微信信息
function ipad_sync_message($_Data) {
	if (empty($_Data) || empty($_Data['id'])) {
		return array (
			'state' => -1,
			'msg'	=> '传入参数有误!',
		);
	}

	$bot_data = simQuery("SELECT * FROM robot WHERE id = {$_Data['id']}");
	if (empty($bot_data) || empty($bot_data['wechat_id'])) {
		return array (
			'state' => -2,
			'msg'	=> '该机器人没有登录微信!',
		);
	}

	// 获取消息
	$result = ipad_get_sync_message($bot_data['wechat_id']);
	LOGINFO($result, '=== $result');
	if (empty($result) || $result['Code'] != 0) {
		return array (
			'state' => -3,
			'msg'	=> '获取数据失败,请重新获取!',
		);
	} else {
		if (empty($result['Data']['AddMsgs'])) {
			return array (
				'state' => 0,
				'msg'	=> '没有获取到消息!',
			);
		} else {
			$is_success = false;
			foreach ($result['Data']['AddMsgs'] as $value) {
				if (strpos($value['Content']['String'], '呼叫小美') !== false && (strpos($value['FromUserName']['String'], '@chatroom') !== false || strpos($value['ToUserName']['String'], '@chatroom') !== false)) {
					if (strpos($value['FromUserName']['String'], '@chatroom') !== false) {
						$group_name = $value['FromUserName']['String'];
					} else {
						$group_name = $value['ToUserName']['String'];
					}
					$from_name = "新建的群";
					$group_data = ipad_get_contract_detail($bot_data['wechat_id'], $group_name);
					// 获取群消息成功,修改群名称
					if ($group_data || $group_data['Code'] == 0) {
						if (!empty($group_data['Data']['contactList'][0]['nickName']['string'])) {
							$from_name = $group_data['Data']['contactList'][0]['nickName']['string'];
						}
					}
					
					$wx_data = simQuery("SELECT * FROM wechat_group WHERE robot_wxid = '{$bot_data['wechat_id']}' AND wechat_room_id = '{$group_name}';");
					if ($wx_data) {
						simQuery("UPDATE wechat_group SET wechat_room_name = '{$from_name}' WHERE robot_wxid = '{$bot_data['wechat_id']}' AND wechat_room_id = '{$group_name}';");
						ipad_send_text_msg($bot_data['wechat_id'], $group_name, '小美正在为您发单');
					} else {
						simQuery("INSERT INTO wechat_group (robot_wxid, wechat_room_id, wechat_room_name, taobao_ance, pdd_ance, jd_ance, invite_code, robot_id, admin_id) VALUES ".
							"('{$bot_data['wechat_id']}', '{$group_name}', '{$from_name}', '{$bot_data['taobao_ance']}', '{$bot_data['pdd_ance']}', '{$bot_data['jd_ance']}', '{$bot_data['invite_code']}', {$bot_data['id']}, {$bot_data['admin_id']});");
						ipad_send_text_msg($bot_data['wechat_id'], $group_name, '小美开始为您发单');
					}
					$is_success = true;

				}
			}
			if ($is_success) {
				return array (
					'state' => 1,
					'msg'	=> '成功!',
				);
			} else {
				return array (
					'state' => 0,
					'msg'	=> '没有监测到呼叫小美!',
				);
			}
		}
	}
}

?>